package com.longbig.multifunction.service;

import com.longbig.multifunction.convert.SwitchConvert;
import com.longbig.multifunction.dto.GecAccountInfo;
import com.longbig.multifunction.dto.GecAccountProjectInfo;
import com.longbig.multifunction.dto.GecProjectRegInfoDO;
import com.longbig.multifunction.dto.ProjectInfo;
import com.longbig.multifunction.helper.ExcelHelper;
import com.longbig.multifunction.mapper.SysGeoAccountInfoMapper;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Administrator
 * @date 2024/6/8 17:10
 * @description
 */
@Service
@Slf4j
public class SyncGecProjectRegInfoServcie {

    @Autowired
    private SwitchConvert switchConvert;

    @Autowired
    private SysGeoAccountInfoMapper sysGeoAccountInfoMapper;

    // 全局变量
    private static Map<String, String> dictMap = new HashMap<>();

    private static final Map<String, String> projectTypeMap = new HashMap<>();
    private static final Map<String, String> powerGenerationTypeMap = new HashMap<>();

    private static final Map<String, Integer> subsidyProjectMap = new HashMap<>();

    private static final Map<String, Integer> projectStatusMap = new HashMap<>();




    public void pushData() throws Exception {
        // 初始化数据
        initializeDictMap();

        File file = new File("D:\\出差绿证项目\\清洗数据\\数据清洗导入\\数据清洗导入\\20240607建档立卡项目数据.xlsx");
        InputStream inputStream = new FileInputStream(file);
        List<ProjectInfo> projectInfos = ExcelHelper.readExcel(inputStream, ProjectInfo.class, 0);
        List<GecProjectRegInfoDO> regInfoDOList = Lists.newArrayList();
        List<GecAccountProjectInfo> accountProjectInfos = Lists.newArrayList();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter formatterNew = DateTimeFormatter.ofPattern("yyyy/M/d H:mm:ss");

        log.info("projectInfos size:{}", projectInfos.size());

        for (ProjectInfo projectInfo : projectInfos) {
            //需要过滤的情况
            String subsidyProject = projectInfo.getSubsidyProject1();
            String gridDateStr = projectInfo.getGridDate();
            if (StringUtils.isEmpty(subsidyProject) || StringUtils.isEmpty(gridDateStr) ||
                    StringUtils.isEmpty(projectInfo.getProvince())
                    || StringUtils.isEmpty(projectInfo.getCity()) || StringUtils.isEmpty(projectInfo.getCounty())
                    || StringUtils.isEmpty(projectInfo.getAddress()) || StringUtils.isEmpty(projectInfo.getOwnerName())
                    || StringUtils.isEmpty(projectInfo.getOwnerCode()) || StringUtils.isEmpty(projectInfo.getOwnerTel())
                    || StringUtils.isEmpty(projectInfo.getProjectType1()) || StringUtils.isEmpty(projectInfo.getProjectSubType1())
                    || StringUtils.isEmpty(projectInfo.getGridName()) || StringUtils.isEmpty(projectInfo.getSubsidyProject1())
                    || Objects.isNull(projectInfo.getProjectRunStatus()) || (projectInfo.getAddress().length() > 255)) {

                continue;
            }




            GecProjectRegInfoDO gecProjectRegInfoDO = switchConvert.GecProjectRegInfo2DO(projectInfo);

            gecProjectRegInfoDO.setRecordStatus(1);
            gecProjectRegInfoDO.setPushToZzStatus(1);
            if (gecProjectRegInfoDO.getGridName() == null) {
                gecProjectRegInfoDO.setGridName("");
            }

            LocalDate date = null;
            try {
                date = LocalDate.parse(gridDateStr, formatterNew);
            } catch (Exception e) {
                log.error("griddate parse error, {}", gridDateStr);
                continue;
            }
            gecProjectRegInfoDO.setGridDate(date);
            gecProjectRegInfoDO.setSysCode(dictMap.get(projectInfo.getProvince()));
            String ownerTypeOrigin = projectInfo.getOwnerTypeOrigin();
            if ("企业".equals(ownerTypeOrigin)) {
                gecProjectRegInfoDO.setOwnerType(2);
            } else if ("个人".equalsIgnoreCase(ownerTypeOrigin)) {
                gecProjectRegInfoDO.setOwnerType(1);
            }
            //发电类型
            String projectType = projectTypeMap.get(projectInfo.getProjectType1());
            if (StringUtils.isEmpty(projectType)) {
                gecProjectRegInfoDO.setProjectType("E");
            } else {
                gecProjectRegInfoDO.setProjectType(projectType);
            }
            //又是啥类型
            String projectSubType = powerGenerationTypeMap.get(projectInfo.getProjectSubType1());
            if (StringUtils.isEmpty(projectSubType)) {
                gecProjectRegInfoDO.setProjectSubType("E");
            } else {
                gecProjectRegInfoDO.setProjectSubType(projectSubType);
            }
            //补贴

            gecProjectRegInfoDO.setSubsidyProject(subsidyProjectMap.get(subsidyProject));
            gecProjectRegInfoDO.setProjectStatus(projectStatusMap.get(projectInfo.getProjectStatus1()));

            regInfoDOList.add(gecProjectRegInfoDO);


            //另一张表
            GecAccountProjectInfo gap = new GecAccountProjectInfo();
            gap.setProjectCode(projectInfo.getProjectCode());
            gap.setOwnerCode(projectInfo.getOwnerCode());
            gap.setOwnerName(projectInfo.getOwnerName());
            LocalDate effectiveBeginTime = null;
            LocalDate effectiveEndTime = null;
            try {
                effectiveBeginTime = LocalDate.parse(gridDateStr, formatterNew);
                effectiveEndTime = LocalDate.parse("9999-01-01", formatter);
            } catch (Exception e) {
                log.error("griddate parse error, {}", gridDateStr);
                continue;
            }
            gap.setEffectiveBeginTime(effectiveBeginTime);
            gap.setEffectiveEndTime(effectiveEndTime);
            gap.setUpdateTime(LocalDateTime.now());
            gap.setCreateTime(LocalDateTime.now());
            accountProjectInfos.add(gap);
        }

        int BATCH_SIZE = 500;
        int size = regInfoDOList.size();
        log.info("过滤之后的总数为：{}", size);
        List<GecProjectRegInfoDO> subList;

        for (int i = 0; i < size; i += BATCH_SIZE) {
            int end = Math.min(i + BATCH_SIZE, size);
            subList = regInfoDOList.subList(i, end);
            log.info("批量插入或更新数据Reginfo:{}", subList.size());
            sysGeoAccountInfoMapper.insertUpdateRegInfo(subList);
        }

        List<GecAccountProjectInfo> subProjects;
        for (int i = 0; i < size; i += BATCH_SIZE) {
            int end = Math.min(i + BATCH_SIZE, size);
            subProjects = accountProjectInfos.subList(i, end);
            log.info("批量插入或更新数据projectInfos:{}", subProjects.size());
            sysGeoAccountInfoMapper.batchInsertAccountProjectInfo(subProjects);
        }


    }



    // 静态方法，用于初始化数据
    private static void initializeDictMap() {
        dictMap.put("北京市", "01BJJY");
        dictMap.put("天津市", "01BJJY");
        dictMap.put("河北省", "01BJJY");
        dictMap.put("黑龙江省", "01BJJY");
        dictMap.put("吉林省", "01BJJY");
        dictMap.put("辽宁省", "01BJJY");
        dictMap.put("陕西省", "01BJJY");
        dictMap.put("青海省", "01BJJY");
        dictMap.put("宁夏回族自治区", "01BJJY");
        dictMap.put("上海市", "01BJJY");
        dictMap.put("安徽省", "01BJJY");
        dictMap.put("西藏自治区", "01BJJY");
        dictMap.put("重庆市", "01BJJY");
        dictMap.put("湖北省", "01BJJY");
        dictMap.put("江西省", "01BJJY");
        dictMap.put("山西省", "01BJJY");
        dictMap.put("山东省", "01BJJY");
        dictMap.put("甘肃省", "01BJJY");
        dictMap.put("新疆生产建设兵团", "01BJJY");
        dictMap.put("新疆维吾尔自治区", "01BJJY");
        dictMap.put("浙江省", "01BJJY");
        dictMap.put("江苏省", "01BJJY");
        dictMap.put("福建省", "01BJJY");
        dictMap.put("河南省", "01BJJY");
        dictMap.put("湖南省", "01BJJY");
        dictMap.put("四川省", "01BJJY");
        dictMap.put("广东省", "02GZJY");
        dictMap.put("广西壮族自治区", "02GZJY");
        dictMap.put("海南省", "02GZJY");
        dictMap.put("云南省", "02GZJY");
        dictMap.put("贵州省", "02GZJY");
    }

    static {
        // 在静态代码块中初始化Map
        projectTypeMap.put("水力发电", "H");
        projectTypeMap.put("风力发电", "W");
        projectTypeMap.put("太阳能发电", "P");
        projectTypeMap.put("一体化项目", "I");
        projectTypeMap.put("生物质发电", "B");
        projectTypeMap.put("地热项目", "G");
        projectTypeMap.put("海洋能", "O");
    }

    static {
        // 初始化Map
        // 水利发电
        powerGenerationTypeMap.put("常规水电", "H");
        powerGenerationTypeMap.put("抽水蓄能", "P");
        // 风力发电
        powerGenerationTypeMap.put("陆上集中式", "C");
        powerGenerationTypeMap.put("陆上分散式", "D");
        powerGenerationTypeMap.put("海上发电", "S");
        // 太阳能发电
        powerGenerationTypeMap.put("光伏电站", "C");
        powerGenerationTypeMap.put("分布式光伏发电", "D");
        powerGenerationTypeMap.put("自然人户用", "F");
        powerGenerationTypeMap.put("太阳能热发电", "S");
        // 生物质能发电
        powerGenerationTypeMap.put("农林生物质直燃", "B");
        powerGenerationTypeMap.put("垃圾焚烧", "W");
        powerGenerationTypeMap.put("沼气发电", "G");
        // 海洋能发电
        powerGenerationTypeMap.put("波浪能发电", "W");
        powerGenerationTypeMap.put("潮汐能发电", "T");
        // 一体化项目
        powerGenerationTypeMap.put("风光一体化", "I");
        // 地热非发电
        powerGenerationTypeMap.put("浅层地热", "S");
        powerGenerationTypeMap.put("中深层地热", "M");
        powerGenerationTypeMap.put("深层地热", "P");
        // 其他
    }

    static {
        subsidyProjectMap.put("补贴项目", 0);
        subsidyProjectMap.put("无补贴项目", 1);
        subsidyProjectMap.put("待纳入补贴项目", 2);
    }


    static {
        projectStatusMap.put("核准备案", 0);
        projectStatusMap.put("开工建设", 1);
        projectStatusMap.put("投产运行", 2);
        projectStatusMap.put("项目退役", 3);
    }
}
